package com.yimils.core.sys.controller;


import com.yimils.core.common.dto.GetQueryParams;
import com.yimils.core.common.dto.RspBase;
import com.yimils.core.sys.dto.DeptDto;
import com.yimils.core.sys.dto.ReqAddDept;
import com.yimils.core.sys.entity.DeptEntity;
import com.yimils.core.sys.service.DeptService;
import com.yimils.core.sys.vo.DeptVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class DeptController {
    @Resource(name = "deptService")
    private DeptService deptService;

    @Autowired
    private DeptDto deptDto;

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"操作成功"}
     * @catalog 系统管理/部门管理
     * @title 添加部门
     * @description 添加部门
     * @json_param {"parentId":null,"name":"招商部","sort":1}
     * @method post
     * @url /dept
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @PostMapping("/dept")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase pubCustomer(@RequestBody ReqAddDept reqParams) {
        if (deptService.createDept(reqParams)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(400, "操作失败");
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"操作成功"}
     * @catalog 系统管理/部门管理
     * @title 获取部门
     * @description 获取部门列表并转换为树形结构
     * @json_param {"parentId":null,"name":"招商部","sort":1}
     * @method post
     * @url /dept
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @GetMapping("/depts")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase getDeptList(GetQueryParams reqParams) {
        RspBase rsp = RspBase.OK();
        DeptVo vo = deptDto.EntityToVo(deptService.getList(reqParams));
        rsp.put("data", vo.getDept());
        return rsp;
    }

    @PutMapping("/dept")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase updateDept(@RequestBody DeptEntity entity) {
        if (deptService.saveOrUpdate(entity)) {
            return RspBase.OK("修改成功");
        }
        return RspBase.ERROR(400, "修改失败");
    }
}
